package com.servlet;

import com.dao.CityDao;
import com.dao.Impl.CityDaoImpl;
import com.dao.Impl.ProvinceDaoImpl;
import com.dao.ProvinceDao;
import com.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(value = "/cityServlet/*")
public class CityServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String url=request.getRequestURI();
        String method=url.substring(url.lastIndexOf("/")+1);
        String target=null;
        switch (method){
            case "findCityId":
                target=findCityId(request,response);
                break;
        }
        if (target!=null){
            request.getRequestDispatcher(target).forward(request,response);
        }
    }


    /**
     * 根据pid查找需要的市
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected String findCityId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pid= Integer.parseInt(request.getParameter("pid"));
        CityDao dao=new CityDaoImpl();
        List<Map> city = dao.findCityPid(pid);
        MyUtil.jsonWriter(response,city);
        return  null;
    }
}
